package com.xhwl.centiotdata.handler.device.impl;

import com.xhwl.centiotdata.handler.device.DeviceIntegrationFactory;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationHandler;
import com.xhwl.centiotdata.mq.producer.GeneralProducer;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.keytoplocal.DeviceKeyTopLocalDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.suppliersystem.keytopLocal.DeviceKeyTopLocalHandler;
import com.xhwl.common.constant.cent.alarm.AlarmConstant;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import com.xhwl.common.enums.device.DeviceTypeEnum;
import com.xhwl.common.pojo.cent.alarm.AlarmManagement;
import com.xhwl.common.pojo.cent.device.Device;
import com.xhwl.common.query.cent.device.DeviceQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author msy
 */
@Slf4j
@Service
public class DeviceIntegrationKeyTopLocalHandler extends DeviceIntegrationHandler {

    @Autowired
    private DeviceKeyTopLocalHandler deviceKeyTopLocalHandler;

    @Autowired
    private IDeviceCentService deviceCentService;

    @Autowired
    private GeneralProducer generalProducer;

    @Override
    public void afterPropertiesSet() throws Exception {
        DeviceIntegrationFactory.register(ProductEnum.KEYTOP_LOCAL.getId(), this);
    }


    @Override
    public void pullDevice(DeviceIntegrationConfigDTO configDTO) {
        // 拉取数据
        List<DeviceKeyTopLocalDTO> deviceKeyTopLocalDTOList = deviceKeyTopLocalHandler.pullDevice(configDTO);
        log.info("deviceKeyTopLocalDTOList: {}", deviceKeyTopLocalDTOList);

        if (CollectionUtils.isEmpty(deviceKeyTopLocalDTOList)) {
            return;
        }

        List<DeviceDTO> deviceDTOList = deviceKeyTopLocalHandler.handleDevice(configDTO, deviceKeyTopLocalDTOList);
        log.info("开始转存设备数据: {}", deviceDTOList);
        // 存储业务中台
        deviceCentService.asyncUpdateDevice(deviceDTOList);
    }


    @Override
    public void pullDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO) {
        if (configDTO.getDeviceType() == null) {
            log.error("projectId：{} pullDeviceOnlineStatus failed! deviceType is null!", configDTO.getProjectId());
            return;
        }

        if(DeviceTypeEnum.DOOR.getCode().equals(configDTO.getDeviceType())) {
            // 拉取数据
            List<DeviceKeyTopLocalDTO> deviceKeyTopLocalDTOList = deviceKeyTopLocalHandler.pullDevice(configDTO);
            if (CollectionUtils.isEmpty(deviceKeyTopLocalDTOList)) {
                return;
            }
            // 转换数据
            List<Device> deviceList = deviceKeyTopLocalHandler.handlePedestrianDeviceOnlineStatus(configDTO, deviceKeyTopLocalDTOList);
            // 设备离线告警
            List<Device> offLineDevices = deviceList.stream().filter(s->s.getOnlineStatus().equals(DeviceOnlineStatusEnum.OFFLINE)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(offLineDevices)) {
                DeviceQuery deviceQuery = new DeviceQuery();
                deviceQuery.setProjectId(configDTO.getProjectId());
                deviceQuery.setThirdDeviceIdList(offLineDevices.stream().map(Device::getThirdDeviceId).collect(Collectors.toList()));
                List<DeviceDTO> deviceDTOList = deviceCentService.listDevice(null, deviceQuery);
                offLineDevices.forEach(device->{
                    AlarmManagement alarmManagement = new AlarmManagement();
                    DeviceDTO deviceDTO = deviceDTOList.stream().filter(s-> StringUtils.equals( s.getThirdDeviceId(), device.getThirdDeviceId()))
                            .findAny().orElse(null);
                    if(deviceDTO != null) {
                        //组装公共部分数据
                        alarmManagement.setProjectId(configDTO.getProjectId());
                        alarmManagement.setAlarmTime(new Date());
                        alarmManagement.setThirdDeviceId(device.getThirdDeviceId());
                        alarmManagement.setDeviceId(deviceDTO.getId());
                        // 门禁离线告警
                        alarmManagement.setAlarmInventoryCode("17798944");
                        generalProducer.send(AlarmConstant.ALARM_TOPIC, alarmManagement);
                    }
                });
            }
            // 存储业务中台
            deviceCentService.asyncUpdateOnlineStatus(deviceList);
        }
    }
}
